package com.gitee.ricesugar.user;

import com.gitee.ricesugar.user.module.AddUser;
import com.gitee.ricesugar.user.module.ListUser;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;

public class UserPanel {
    private static JPanel contentPanel;

    public static JPanel createUserPanel() throws IOException {
        //新建主布局
        JPanel userPanel = new JPanel();
        userPanel.setLayout(new BorderLayout());

        //添加侧边栏
        JPanel sidebar = new JPanel();
        sidebar.setLayout(new GridLayout(2, 1));

        JButton addUserButton = new JButton("添加用户");
        JButton userListButton = new JButton("用户列表");
        addUserButton.setPreferredSize(new Dimension(96, 40));

        sidebar.add(addUserButton);
        sidebar.add(userListButton);

        //添加每个页面
        contentPanel = new JPanel();
        contentPanel.setLayout(new CardLayout());
        JPanel addUserPage = new AddUser();
        JPanel userListPage = new ListUser();
        contentPanel.add(addUserPage, "addUserPage");
        contentPanel.add(userListPage, "userListPage");
        showPanel("userListPage");

        addUserButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanel("addUserPage");
            }
        });
        userListButton.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                showPanel("userListPage");
            }
        });

        userPanel.add(sidebar, BorderLayout.WEST);
        userPanel.add(contentPanel, BorderLayout.CENTER);

        //返回主布局
        return userPanel;
    }

    private static void showPanel(String panelName) {
        CardLayout cardLayout = (CardLayout) contentPanel.getLayout();
        cardLayout.show(contentPanel, panelName);
    }
}
